//
//  WBFristPageCellHeadView.swift
//  weibo
//
//  Created by  郭航 on 16/6/27.
//  Copyright © 2016年 郭航. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

let cellMargin: CGFloat = 10

class WBFristPageCellHeadView: UIView {
    
    var dataModel: WBFristPageDataModel? {
        didSet {
            iconImageView.sd_setImageWithURL(NSURL(string: dataModel?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default"))
            avatarVipImageView.image = dataModel?.user?.verifiedImage
            nameLabel.text = dataModel?.user?.screen_name
            vipImageView.image = dataModel?.user?.mbrankImage
            timeLabel.text = dataModel?.createdStr
            sourceLabel.attributedText = dataModel?.sourceStr
            textLabel.attributedText = dataModel?.textAttr
            
            if dataModel?.pic_urls?.count > 0 {
                pictureView.dataModel = dataModel?.pic_urls
                
                selfBottomConstraints?.uninstall()
                self.snp_makeConstraints { (make) in
                    selfBottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                }
                
                pictureView.hidden = false
            } else {
                pictureView.hidden = true
                
                selfBottomConstraints?.uninstall()
                self.snp_makeConstraints { (make) in
                    selfBottomConstraints = make.bottom.equalTo(textLabel.snp_bottom).offset(cellMargin).constraint
                }
            }
        }
    }
    
    private var selfBottomConstraints: Constraint?
    
    private lazy var iconImageView = UIImageView()
    
    private lazy var avatarVipImageView = UIImageView(imageName: "avatar_vgirl")
    
    private lazy var nameLabel = UILabel(textColor: UIColor.darkGrayColor(), textAlignment: .Left, font: 15)
    
    private lazy var vipImageView = UIImageView(imageName: "common_icon_membership_expired")
    
    private lazy var timeLabel = UILabel(textColor: APPTintColor, textAlignment: .Left, font: 10)

    private lazy var sourceLabel = UILabel(textColor: UIColor.grayColor(), textAlignment: .Left, font: 10)
    
    private lazy var textLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textAlignment: .Left, font: 15, maxWidth: SCREENW - 2 * cellMargin)
    
    private lazy var pictureView: WBFristPagePictureView = WBFristPagePictureView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()

        addSubview(iconImageView)
        
        iconImageView.snp_makeConstraints { (make) in
            make.leading.top.equalTo(self).offset(cellMargin)
            make.width.height.equalTo(35) 
        }
        
        addSubview(avatarVipImageView)
        
        avatarVipImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconImageView.snp_trailing)
            make.centerY.equalTo(iconImageView.snp_bottom)
        }
        
        addSubview(nameLabel)
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImageView)
            make.leading.equalTo(iconImageView.snp_trailing).offset(cellMargin)
        }
        
        addSubview(vipImageView)
        
        vipImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(cellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        addSubview(timeLabel)
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconImageView)
        }
        
        addSubview(sourceLabel)
        
        sourceLabel.snp_makeConstraints { (make) in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(cellMargin)
        }
        
        addSubview(textLabel)
        
        textLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImageView.snp_bottom).offset(cellMargin)
            make.leading.equalTo(self).offset(cellMargin)
        }
       
        addSubview(pictureView)
        
        pictureView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(textLabel.snp_bottom).offset(cellMargin)
        }
        
        self.snp_makeConstraints { (make) in
             selfBottomConstraints = make.bottom.equalTo(textLabel.snp_bottom).offset(cellMargin).constraint
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
